* Figure 10: Distribution of mean village outcomes by village-year treatment status (OLS regressions)
* Sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if ex_post_balp==1

* Treatment Expansion not included here, no control countergroup
drop if c_b2009==1

* Easier labelling for graphs
la var w_real_sav "savings (INR)"
la var w_real_lent "money lent out (INR)"
la var w_real_borrow "money borrowed (INR)"
la var w_real_cons "total consumption (INR)"
la var w_real_food_exp "food expenditure (INR)"
la var w_real_nonfood_exp "non-food expenditure (INR)"
la var w_real_durables_exp "durables expenditure (INR)"
la var w_real_events_exp "events expenditure (INR)"
la var w_real_income "annual income(INR)"
la var w_real_gifts_out "gifts/transfers made (INR)"
la var w_real_gifts_in "gifts/transfers received (INR)"
la var food_sufficiency_child "food sufficiency for children"
la var fladder_std "financial situation (sd units)"
la var outlook_std "good things happen (sd units)"
la var control_std "control over life (sd units)"

replace treat_year=0 if mkt_year==2005
sort id mkt_year

// CDFs
foreach var of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
areg `var', a(mkt_year)
predict e`var'yhat, residuals
qui sum `var'
gen `var'yhat = r(mean)+e`var'yhat
bys villageno: egen vill`var' = mean(`var'yhat)
}

foreach var of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
cumul vill`var' if treat_year == 1, gen(CDF_`var'_T) equal
cumul vill`var' if treat_year == 0, gen(CDF_`var'_C) equal
lab var CDF_`var'_T "Treatment Group"
lab var CDF_`var'_C "Control Group"
}

* Epps-Singleton
foreach var of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child {
local l`var': variable label `var'
preserve
ksmirnov vill`var', by(treat_year)
gen p_cor = r(p_cor) 
keep p_cor
duplicates drop p_cor, force
gen a = string(p_cor, "%7.2f")
local a = a
restore
preserve
escftest vill`var', group(treat_year)
gen p_val=r(p_val)
keep p_val
duplicates drop p_val, force
gen p = string(p_val, "%7.2f")
local b = p
restore
twoway (line CDF_`var'_T CDF_`var'_C vill`var', sort), title("`l`var''", size(s)) ylabel(,angle (30)) xlabel(,angle (30)) xtitle("") note("KS Test, `a' | Epps-Singleton, `b'") graphregion(color(white)) plotregion(icolor(white)) 
graph save cdfs`var', replace 
}

foreach var of varlist fladder_std outlook_std control_std {
local l`var': variable label `var'
preserve
ksmirnov vill`var', by(treat_year)
gen p_cor = r(p_cor) 
keep p_cor
duplicates drop p_cor, force
gen a = string(p_cor, "%7.2f")
local a = a
restore
preserve
escftest vill`var', group(treat_year)
gen p_val=r(p_val)
keep p_val
duplicates drop p_val, force
gen p = string(p_val, "%7.2f")
local b = p
restore
twoway (line CDF_`var'_T CDF_`var'_C vill`var', sort), title("`l`var''", size(s)) ylabel(,angle (30)) xlabel(-1 0 1,angle (30)) xtitle("") note("KS Test, `a' | Epps-Singleton, `b'") graphregion(color(white)) plotregion(icolor(white)) 
graph save cdfs`var', replace 
}

* combining graphs
grc1leg cdfsw_real_sav.gph cdfsw_real_lent.gph cdfsw_real_borrow.gph cdfsw_real_cons.gph cdfsw_real_food_exp.gph cdfsw_real_nonfood_exp.gph cdfsw_real_durables_exp.gph cdfsw_real_events_exp.gph  cdfsw_real_gifts_out.gph cdfsw_real_income.gph cdfsw_real_gifts_in.gph cdfsfood_sufficiency_child.gph cdfsfladder_std.gph cdfsoutlook_std.gph cdfscontrol_std.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../output/graphs/f10.emf", replace


